﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuoiHoi.Business;
using CuoiHoi.Entities;

namespace CuoiHoi.Admin
{
    public partial class ucQuanLyChucVu : System.Web.UI.UserControl
    {
        public bool isClick = false;
        static int idChucVu1 = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDaTa();
            if (!IsPostBack)
            {  
                LoadQuyen();
            }
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            string chon = "";
            for (int i = 0; i < CheckBoxQuyen.Items.Count; i++)
            {
                if (CheckBoxQuyen.Items[i].Selected)
                {
                    chon += CheckBoxQuyen.Items[i].Value + " ";
                }
            }
            BChucVu.Insert(new EChucVu
            {
                TenChucVu = txtTenChucVu.Text,
                ChuoiQuyen = chon.Trim()
            });
            LoadDaTa();
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            string chon1 = "";
            for (int i = 0; i < CheckBoxQuyen.Items.Count; i++)
            {
                if (CheckBoxQuyen.Items[i].Selected)
                {
                    chon1 += CheckBoxQuyen.Items[i].Value + " ";
                }
            }

            BChucVu.Update(new EChucVu
            {
                idChucVu = idChucVu1,
                TenChucVu = txtTenChucVu.Text,
                ChuoiQuyen = chon1.Trim()
            });
            BoCheckbox();
            LoadDaTa();
            Session["action"] = 2;
        }

        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        } 

        void LoadDaTa()
        {
            grvChucVu.AutoGenerateColumns = false;
            grvChucVu.DataSource = BChucVu.SelectAll();
            grvChucVu.DataBind();
        }

        void LoadQuyen()
        {
            CheckBoxQuyen.DataSource = BQuyen.SelectAll();
            CheckBoxQuyen.DataTextField = "TenQuyen";
            CheckBoxQuyen.DataValueField = "idQuyen";
            CheckBoxQuyen.DataBind();
        }

        protected void grvChucVu_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    LinkButton btDelete = (LinkButton)e.Row.Cells[4].Controls[0];
                    if (btDelete != null)
                    {
                        btDelete.Attributes.Add("OnClick", "javascript:return confirm('Bạn có muốn xóa')");
                    }
                }
                catch { }
            }
        }

        protected void grvChucVu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {      
                // găn checknen
                BoCheckbox();
               int index = int.Parse(e.CommandArgument.ToString());
               idChucVu1 =int.Parse(grvChucVu.Rows[index].Cells[0].Text);
               string chon = grvChucVu.Rows[index].Cells[2].Text ;
               //ThongBao(chon);
               string [] mang = chon.Split(' ');
               if (mang != null && mang[0].ToString() != "&nbsp;")
               {
                   foreach (var item in mang)
                   {
                       int vitri = int.Parse(item);
                       for (int i = 0; i < CheckBoxQuyen.Items.Count; i++)
                       {
                           if (CheckBoxQuyen.Items[i].Value == item)
                           {
                               CheckBoxQuyen.Items[i].Selected = true;
                           }
                       }
                   }
               }              
               txtTenChucVu.Text = HttpUtility.HtmlDecode(grvChucVu.Rows[index].Cells[1].Text);
                isClick = true;
            }
        }

        void BoCheckbox()
        {
            for (int i = 0; i < CheckBoxQuyen.Items.Count; i++)
            {
                CheckBoxQuyen.Items[i].Selected = false;
            }
            txtTenChucVu.Text = "";
        }

        protected void grvChucVu_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {
            BChucVu.Delete(int.Parse(grvChucVu.DataKeys[e.RowIndex].Value.ToString()));
            Session["action"] = 3;
            LoadDaTa();
        }
    }
}